home *** CD-ROM | disk | FTP | other *** search
/ Delphi Programmer's Power Pack / Delphi Volume 1.iso / s_to_z / spidr100 / setup.arv / LISTTEST.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-09-15  |  9.4 KB  |  374 lines

  1. {----------------------------------------------------------------------------
  2. |
  3. | Library: Spider Containers for Object Pascal
  4. |
  5. | Module: ListTest.Pas
  6. |
  7. | Description: Form for TNodeContainer and derived classes test.
  8. |
  9. | History: Version 1.0  March 1996. Copyright (c) 1996 Michel Brazeau
  10. |                                   Interval Software
  11. |
  12. |---------------------------------------------------------------------------}
  13. unit ListTest;
  14.  
  15. interface
  16.  
  17. uses
  18.  
  19.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  20.   Forms, Dialogs, StdCtrls,
  21.  
  22.   ObjBase;  { TNodeContainer }
  23.  
  24. const
  25.     { list indexes related to ListTypeCombo's ItemIndex property. The
  26.       strings in ListTypeCombo were set at design time matching these
  27.       constants }
  28.     liUnOrderedList = 0;
  29.     liOrderedList   = 1;
  30.     liBinaryTree    = 2;
  31.  
  32. type
  33.   TListIndex = liUnOrderedList .. liBinaryTree;
  34.  
  35. type
  36.   TNodeContainerForm = class(TForm)
  37.     AddRandomButton: TButton;
  38.     RemoveButton: TButton;
  39.     ClearButton: TButton;
  40.     ListBox: TListBox;
  41.     ListTypeCombo: TComboBox;
  42.     ItemCount: TLabel;
  43.     AddButton: TButton;
  44.     Load: TButton;
  45.     SearchButton: TButton;
  46.     procedure FormDestroy(Sender: TObject);
  47.     procedure RemoveButtonClick(Sender: TObject);
  48.     procedure AddRandomButtonClick(Sender: TObject);
  49.     procedure ClearButtonClick(Sender: TObject);
  50.     procedure ListBoxDrawItem(Control: TWinControl; Index: Integer;
  51.       Rect: TRect; State: TOwnerDrawState);
  52.     procedure FormCreate(Sender: TObject);
  53.     procedure ListTypeComboChange(Sender: TObject);
  54.     procedure AddButtonClick(Sender: TObject);
  55.     procedure LoadClick(Sender: TObject);
  56.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  57.     procedure SearchButtonClick(Sender: TObject);
  58.  
  59.   private
  60.      List : TNodeContainer;
  61.  
  62.      { returns the combo box index represented by the current list
  63.        type. }
  64.      function CurrentListIndex : TListIndex;
  65.  
  66.      { creates the List data member given the list type }
  67.      procedure CreateList(Index : TListIndex);
  68.  
  69.      { updates the ItemCount lable from the List.Size }
  70.      procedure UpdateItemCount;
  71.  
  72.      { add a value to the list }
  73.      procedure AddValue(Value : Word);
  74.  
  75. end;
  76.  
  77. {--------------------------------------------------------------------------}
  78.  
  79. implementation
  80.  
  81. {$R *.DFM}
  82.  
  83. uses
  84.       ObjTest,     { GetRandomValue }
  85.       ObjBuckt,    { TBucket }
  86.       ObjList,     { TOrderedDblList }
  87.       ObjBTree;    { TBinaryTree }
  88.  
  89.  
  90. {--------------------------------------------------------------------------}
  91.  
  92. procedure TNodeContainerForm.FormCreate(Sender: TObject);
  93. begin
  94.     { by default, create an unordered list }
  95.     CreateList(liUnOrderedList);
  96.  
  97.     ListTypeCombo.ItemIndex := liUnorderedList;
  98. end;
  99.  
  100. {--------------------------------------------------------------------------}
  101.  
  102. procedure TNodeContainerForm.FormDestroy(Sender: TObject);
  103. begin
  104.     List.Free;
  105. end;
  106.  
  107. {--------------------------------------------------------------------------}
  108.  
  109. procedure TNodeContainerForm.AddValue(Value : Word);
  110. var
  111.     Bucket : TWordBucket;
  112. begin
  113.     Bucket := TWordBucket.Create(Value);
  114.     try
  115.         ListBox.Enabled := False;
  116.         try
  117.             ListBox.Items.AddObject('', nil);
  118.             try
  119.                 List.Insert(Bucket);
  120.             except
  121.                 ListBox.Items.Delete(ListBox.Items.Count-1);
  122.                 raise;
  123.             end;
  124.         finally
  125.             ListBox.Enabled := True;
  126.         end;
  127.     except
  128.         Bucket.Free;
  129.         raise;
  130.     end;
  131. end;
  132.  
  133. {--------------------------------------------------------------------------}
  134.  
  135. procedure TNodeContainerForm.AddRandomButtonClick(Sender: TObject);
  136. begin
  137.     AddValue(GetRandomNumber);
  138.  
  139.     ListBox.ItemIndex := ListBox.Items.Count - 1;
  140.  
  141.     UpdateItemCount;
  142. end;
  143.  
  144. {--------------------------------------------------------------------------}
  145.  
  146. procedure TNodeContainerForm.RemoveButtonClick(Sender: TObject);
  147. var
  148.     Item : LongInt;  { 0 based item index }
  149.  
  150. begin
  151.     Item := ListBox.ItemIndex;
  152.  
  153.     if Item <= -1 then
  154.         Exit;  { no item is selected, since ItemIndex = -1 when no item is
  155.                  selected }
  156.  
  157.     ListBox.Enabled := False;
  158.  
  159.     List.GotoNth(Item+1);
  160.     List.DeleteCurrent;
  161.  
  162.     try
  163.         ListBox.Items.Delete(Item);
  164.  
  165.         { keep an item selected, convert from 1 based to 0 based }
  166.         if ListBox.Items.Count <= Item then
  167.             ListBox.ItemIndex := Item - 1
  168.         else
  169.             ListBox.ItemIndex := Item;
  170.     finally
  171.         ListBox.Enabled := True;
  172.     end;
  173.  
  174.     UpdateItemCount;
  175. end;
  176.  
  177. {--------------------------------------------------------------------------}
  178.  
  179. procedure TNodeContainerForm.ClearButtonClick(Sender: TObject);
  180. begin
  181.     { clear the list box }
  182.     ListBox.Clear;
  183.  
  184.     { clear the node container }
  185.     List.Clear;
  186.  
  187.     UpdateItemCount;
  188. end;
  189.  
  190. {--------------------------------------------------------------------------}
  191.  
  192. procedure TNodeContainerForm.ListBoxDrawItem(Control: TWinControl; Index: Integer;
  193.   Rect: TRect; State: TOwnerDrawState);
  194. begin
  195.     with (Control as TListBox).Canvas do
  196.     begin
  197.         FillRect(Rect);    { clear the rectangle }
  198.  
  199.         TextOut( Rect.Left + 2, Rect.Top,
  200.                  IntToStr((List[Index+1] as TWordBucket).Value))
  201.     end; { with }
  202. end;
  203.  
  204. {--------------------------------------------------------------------------}
  205.  
  206. procedure TNodeContainerForm.ListTypeComboChange(Sender: TObject);
  207. var
  208.     CurIndex : Word;
  209. begin
  210.     CurIndex := CurrentListIndex;
  211.  
  212.     with Sender as TComboBox do
  213.     begin
  214.         if ItemIndex <> CurIndex then
  215.         begin
  216.             List.Free;
  217.  
  218.             CreateList(ItemIndex);
  219.  
  220.             ListBox.Clear;
  221.  
  222.             UpdateItemCount;
  223.         end;
  224.     end;
  225. end;
  226.  
  227. {--------------------------------------------------------------------------}
  228.  
  229. function TNodeContainerForm.CurrentListIndex : TListIndex;
  230. begin
  231.     if List is TUnorderedList then
  232.         Result := liUnorderedList
  233.     else if List is TOrderedList then
  234.         Result := liOrderedList
  235.     else
  236.         Result := liBinaryTree;
  237. end;
  238.  
  239. {--------------------------------------------------------------------------}
  240.  
  241. procedure TNodeContainerForm.CreateList(Index : TListIndex);
  242. begin
  243.     case Index of
  244.     liUnOrderedList : List := TUnOrderedList.Create(TWordBucket, CompareWordBucket);
  245.     liOrderedList   : List := TOrderedList.Create(TWordBucket, CompareWordBucket);
  246.     liBinaryTree    : List := TBinaryTree.Create(TWordBucket, CompareWordBucket);
  247.     end; { case }
  248. end;
  249.  
  250. {--------------------------------------------------------------------------}
  251.  
  252. procedure TNodeContainerForm.UpdateItemCount;
  253. begin
  254.     ItemCount.Caption := IntToStr(List.Size);
  255. end;
  256.  
  257. {--------------------------------------------------------------------------}
  258.  
  259. procedure TNodeContainerForm.AddButtonClick(Sender: TObject);
  260. const
  261.     NumStr : String = '0';
  262.  
  263. begin
  264.     if not InputQuery('', 'Value to add: ', NumStr) then
  265.         Exit;
  266.  
  267.     AddValue(StrToInt(NumStr));
  268.  
  269.     ListBox.ItemIndex := ListBox.Items.Count - 1;
  270.  
  271.     UpdateItemCount;
  272. end;
  273.  
  274. {--------------------------------------------------------------------------}
  275.  
  276. procedure TNodeContainerForm.LoadClick(Sender: TObject);
  277. var
  278.     NumberList : TUnorderedList;
  279.  
  280.     WordBucket : TWordBucket;
  281.  
  282.     Value      : Word;
  283.  
  284.     I          : LongInt;
  285.  
  286. begin
  287.     NumberList := TUnOrderedList.Create(TWordBucket, CompareWordBucket);
  288.     try
  289.         ListBox.Enabled := False;
  290.  
  291.         TestForm.LoadNumbersFromFile(NumberList);
  292.  
  293.         Screen.Cursor := crHourGlass;
  294.  
  295.         try
  296.             I := 1;
  297.  
  298.             { insert all the values in NumberList }
  299.             if NumberList.GotoFirst then
  300.             repeat
  301.                 { give other applications processing time }
  302.                 if (I mod 500) = 0 then
  303.                     Application.ProcessMessages;
  304.                 Inc(I);
  305.  
  306.                 Value := (NumberList.CurrentObj as TWordBucket).Value;
  307.  
  308.                 WordBucket := TWordBucket.Create(Value);
  309.  
  310.                 AddValue(Value);
  311.  
  312.            until not NumberList.GotoNext;
  313.  
  314.         finally
  315.             Screen.Cursor := crDefault;
  316.  
  317.             ListBox.Enabled := True;
  318.         end;
  319.  
  320.     finally
  321.         NumberList.Free;
  322.  
  323.         ListBox.ItemIndex := ListBox.Items.Count - 1;
  324.  
  325.         UpdateItemCount;
  326.     end;
  327. end;
  328.  
  329. {--------------------------------------------------------------------------}
  330.  
  331. procedure TNodeContainerForm.FormClose(Sender: TObject;
  332.   var Action: TCloseAction);
  333. begin
  334.     Action := caFree;
  335. end;
  336.  
  337. {--------------------------------------------------------------------------}
  338.  
  339. procedure TNodeContainerForm.SearchButtonClick(Sender: TObject);
  340. const
  341.     NumberStr : String = '0';
  342.  
  343.     OccurStr  : String = '1';
  344.  
  345. var
  346.     Occur     : LongInt;
  347.  
  348.     Bucket    : TWordBucket;
  349.  
  350. begin
  351.     if not InputQuery('', 'Search for : ', NumberStr) then
  352.         Exit;
  353.  
  354.     if not InputQuery('', 'Occurence : ', OccurStr) then
  355.         Exit;
  356.  
  357.     Occur := StrtoInt(OccurStr);
  358.  
  359.     Bucket := TWordBucket.Create(StrToInt(NumberStr));
  360.     try
  361.         if List.Search(Bucket, Occur) then
  362.             MessageDlg('Value found', mtInformation,[mbOk], 0)
  363.         else
  364.             MessageDlg('Value NOT found!', mtInformation,[mbOk], 0);
  365.  
  366.     finally
  367.         Bucket.Free;
  368.     end;
  369. end;
  370.  
  371. {--------------------------------------------------------------------------}
  372.  
  373. end.
  374.